import mysql from "mysql2";
import config from "./db.config.js";

// 创建连接池
const pool = mysql.createPool({
  host: config.HOST,
  user: config.USER,
  password: config.PASSWORD,
  database: config.DB,
  waitForConnections: true,
  connectionLimit: config.pool.max,
  queueLimit: 0
});

// 获取连接池中的连接
const getConnection = () => {
  return new Promise((resolve, reject) => {
    pool.getConnection((err, connection) => {
      if (err) {
        reject(err);
      } else {
        resolve(connection);
      }
    });
  });
};

export default {
  pool,
  getConnection
};